{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7ed56a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "IMAGE_ROOT = '/data/user/zhangdonghao/openCV_test/Template'\n",
    "\n",
    "# 封装成小函数\n",
    "def cv_show(name, img):\n",
    "    cv2.imshow(name, img)\n",
    "    cv2.waitKey()\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05f0c554",
   "metadata": {},
   "source": [
    "##### Canny边缘检测"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90f45d8c",
   "metadata": {},
   "source": [
    "* 1）使用高斯滤波器，以平滑图像，滤除噪声。\n",
    "* 2）计算图像中每个像素点的梯度强度和方向。\n",
    "* 3）使用非极大值（Non-Maximum Suppression）抑制，以消除边缘检测带来的杂散响应。\n",
    "* 4）应用双阈值（Double-Threshold）检测来确定真实的和潜在的边缘。\n",
    "* 5）通过抑制孤立的弱边缘最终完成边缘检测。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a803786",
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread(os.path.join(IMAGE_ROOT, 'fenli.png'), cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "v1 = cv2.Canny(img, 80, 150) \n",
    "v2 = cv2.Canny(img, 50, 100) \n",
    "\n",
    "# 后两个参数是minVal和maxVal，规则是： \n",
    "# 梯度 > maxVal => 视为边界\n",
    "# maxVal > 梯度 > minVal => 若连有边界，则保留\n",
    "# minVal > 梯度 => 直接舍弃\n",
    "\n",
    "res = np.hstack((v1, v2))\n",
    "cv_show(\"res\", res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03085340",
   "metadata": {},
   "outputs": [],
   "source": [
    "img1 = cv2.imread(os.path.join(IMAGE_ROOT, 'protossleader.png'), cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "v1 = cv2.Canny(img1, 120, 250)\n",
    "v2 = cv2.Canny(img1, 50, 100)\n",
    "\n",
    "res = np.hstack((v1, v2))\n",
    "cv_show(\"res\", res)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
